﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class InstructorAnnouncementsMainScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindAnnouncements(  );
            }
        }

        protected void OnBtnAddClicked(object sender, EventArgs e)
        {
            Announcement _announcement = new Announcement
                                             {
                                                 Entry = txtEntry.Text,
                                                 AnnouncementTime = DateTime.Now,
                                                 Subject = txtSubject.Text
                                             };
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            _currentCourse.AddAnnouncement(_announcement);

            NotifyUsers(txtEntry.Text, txtSubject.Text);
            
            txtEntry.Text = string.Empty;
            txtSubject.Text = string.Empty;

            BindAnnouncements(  );
        }

        private void BindAnnouncements()
        {
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            dtgAnnouncements.DataSource = _currentCourse.Announcements;
            dtgAnnouncements.DataBind();
        }

        protected void OnDtgAnnouncementCancellingEdit( object sender, GridViewCancelEditEventArgs e )
        {
            dtgAnnouncements.EditIndex = -1;
            BindAnnouncements(  );
        }

        protected void OnDtgAnnouncementsRowEditing( object sender, GridViewEditEventArgs e )
        {
            dtgAnnouncements.EditIndex = e.NewEditIndex;
            BindAnnouncements(  );
        }

        protected void OnDtgAnnouncementsRowCommand( object sender, GridViewCommandEventArgs e )
        {
            //TODO:Nothing.
        }

        protected void OnDtgAnnouncementsRowUpdating( object sender, GridViewUpdateEventArgs e )
        {
            dtgAnnouncements.Rows[ e.RowIndex ].Focus( );
            int _announcementId = CommonMethods.GetIdFromGridViewRow( dtgAnnouncements.Rows[ e.RowIndex ] );

            TextBox _txtEntryEdit = ( TextBox ) dtgAnnouncements.Rows[ e.RowIndex ].FindControl( "txtEntryEdit" );
            TextBox _txtSubjectEdit = ( TextBox ) dtgAnnouncements.Rows[ e.RowIndex ].FindControl( "txtSubjectEdit" );

            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            Announcement _announcement = new Announcement
                                             {
                                                 AnnouncementId = _announcementId,
                                                 Entry = _txtEntryEdit.Text,
                                                 Subject = _txtSubjectEdit.Text
                                             };

            _currentCourse.UpdateAnnouncement( _announcement );
            dtgAnnouncements.EditIndex = -1;
            BindAnnouncements( );
        }

        protected void OnDtgAnnouncementsRowDeleting( object sender, GridViewDeleteEventArgs e )
        {
            int _announcementId = CommonMethods.GetIdFromGridViewRow(dtgAnnouncements.Rows[e.RowIndex]);
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            _currentCourse.DeleteAnnouncement( _announcementId );
            BindAnnouncements(  );
        }

        private void NotifyUsers(string entry, string subject)
        {
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);

            string _body = "<b>Course Name: </b>" + _currentCourse.CourseName +"<br/><b>Announcement Subject: </b>" + subject + "<br/><b>Announcement Entry: </b>" + entry;
            const string mailSubject = "New Announcement is added!";

            List<Student> _students = _currentCourse.Students;
            string _mailAdresses = string.Empty;

            foreach (var _student in _students)
            {
                if (!string.IsNullOrEmpty(_student.Email) && _student.Email.Contains("@"))
                {
                    _mailAdresses += _student.Email + ";";
                }
            }

            Email _email = new Email(mailSubject, _body, _mailAdresses);
            _email.SendEmail();
        }
    }
}